<?php

$get = array_merge($_GET, $_POST);

$inline = $get["inline"];
$text = trim($get["text"]);

if($text){
	$width = (int)$get["width"];
	$height = (int)$get["height"];
	$padding = $get["padding"];
	$fontSize = (int)$get["fontsize"];
	preg_match_all('/(\d+)/', $get["color"], $color);
	$color = $color[1];
	preg_match_all('/(\d+)/', $get["background"], $background);
	$background = $background[1];
	$textAlign = strtolower($get["textalign"]); $textAlign = in_array($textAlign, array("right", "center")) ? $textAlign : "left";
	
	$bg = new ImagickPixel($get["background"]);
	$fg = new ImagickPixel($get["color"]);
	
	$img = new Imagick();
	$draw = new ImagickDraw();

	$draw->setFillColor($fg->getColorAsString());
	$draw->setFont("../geoslab.ttf");
	$draw->setFontSize($fontSize);
	$metrics = $img->queryFontMetrics($draw, $text);
	$bbox_width = $metrics["textWidth"];
	$bbox_height = $metrics["textHeight"];
	if($bbox_width>$width) $width=$bbox_width;
	if($bbox_height>$height) $height=$bbox_height;
	$img->newImage($width, $height, $bg);
	
	$left = 0;
	$top = $fontSize+$metrics["descender"] + 1;
	switch($textAlign){
		case "right":
			$left = $width - $bbox_width - 1;
			break;
		case "center":
			$left = round(($width - $bbox_width)/2);
			break;
	}
	$img->annotateImage($draw, $left, $top, 0, $text);
	$img->setImageFormat("png");
	
	ob_start();
	ob_clean();
	
	if($inline) header("Content-Type:image/png");
	
	echo $img;
	
	if(!$inline){
		$data = ob_get_contents();
		ob_clean();
		echo base64_encode($data);
	}
}

?>